package com.ruoyi.station.controller;

import java.util.List;
import java.util.Arrays;
import java.util.Map;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.station.domain.QueryData;
import com.ruoyi.station.domain.SDistributionFrame;
import com.ruoyi.station.domain.SLineManagement;
import com.ruoyi.station.service.SLineManagementService;
import com.ruoyi.station.util.ParseSql;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.multipart.MultipartFile;

/**
 * 线路管理Controller
 *
 * @author ruoyi
 * @date 2021-08-09
 */
@RestController
@RequestMapping("/management")
@Api(value="线路管理对象操作资源", tags = "线路管理对象操作资源")
public class SLineManagementController extends BaseController
{
    @Autowired
    private SLineManagementService sLineManagementService;

    @Autowired
    private TokenService tokenService;
    @Autowired
    private ISysUserService iSysUserService;

    /**
     * 查询线路管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:management:list')")
    @PostMapping("/list")
    @ApiOperation(value="查询配线架列表", notes="查询配线架列表")
    public AjaxResult query(@ApiParam @RequestBody Map map)
    {
        JSONArray queryData = JSONUtil.parseArray(map.get("queryData"));
        QueryWrapper<SLineManagement> wrapper = new QueryWrapper<>();
        List<String> userList = iSysUserService.selectUserByDept(SecurityUtils.getLoginUser().getUser().getDeptId());
        List<SLineManagement> list = sLineManagementService.list(ParseSql.parse(wrapper, JSONUtil.toList(queryData, QueryData.class),userList));
        return AjaxResult.success(list);
    }


    /**
     * 查询线路管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:management:query')")
    @GetMapping("/list")
    @ApiOperation(value="查询线路管理列表", notes="查询线路管理列表")
    public AjaxResult list(@ApiParam String id)
    {
        List<SLineManagement> sLineManagements = sLineManagementService.list(new LambdaQueryWrapper<SLineManagement>()
                .eq(SLineManagement::getDistributionFrameId , id));
        return AjaxResult.success(sLineManagements);
    }

    /**
     * 导出线路管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:management:export')")
    @Log(title = "线路管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @ApiOperation(value="导出线路管理列表", notes="导出线路管理列表")
    public AjaxResult export(@ApiParam SLineManagement sLineManagement)
    {
        QueryWrapper<SLineManagement> wrapper = new QueryWrapper();
        List<SLineManagement> list = sLineManagementService.list(wrapper);
        ExcelUtil<SLineManagement> util = new ExcelUtil<SLineManagement>(SLineManagement.class);
        return util.exportExcel(list, "management");
    }

    /**
     * 获取线路管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:management:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value="获取线路管理详细信息", notes="获取线路管理详细信息")
    public AjaxResult getInfo(@ApiParam @PathVariable("id") String id)
    {
        return AjaxResult.success(sLineManagementService.getById(id));
    }

    /**
     * 新增线路管理
     */
    @PreAuthorize("@ss.hasPermi('system:management:add')")
    @Log(title = "线路管理", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value="新增线路管理", notes="新增线路管理")
    public AjaxResult add(@ApiParam @RequestBody SLineManagement sLineManagement)
    {
        sLineManagement.setCreateBy(SecurityUtils.getUsername());
        int result = sLineManagementService.save(sLineManagement) == true ? 1 : 0;
        return toAjax(result);
    }

    /**
     * 修改线路管理
     */
    @PreAuthorize("@ss.hasPermi('system:management:edit')")
    @Log(title = "线路管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value="修改线路管理", notes="修改线路管理")
    public AjaxResult edit(@ApiParam @RequestBody SLineManagement sLineManagement)
    {
        int result = sLineManagementService.updateById(sLineManagement) == true ? 1 : 0;
        return toAjax(result);
    }

    /**
     * 删除线路管理
     */
    @PreAuthorize("@ss.hasPermi('system:management:remove')")
    @Log(title = "线路管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value="删除线路管理", notes="删除线路管理")
    public AjaxResult remove(@ApiParam @PathVariable String[] ids)
    {
        List<String> list = Arrays.asList(ids);
        int result = sLineManagementService.removeByIds(list) == true ? 1 : 0;
        return toAjax(result);
    }

    /**
     * 导出线路管理模板
     * @return
     */
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<SLineManagement> util = new ExcelUtil<>(SLineManagement.class);
        return util.importTemplateExcel("线路管理属性数据");
    }

    @Log(title = "导入线路管理管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:management:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<SLineManagement> util = new ExcelUtil<>(SLineManagement.class);
        List<SLineManagement> sLineManagements = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String message = sLineManagementService.importsSLineManagement(sLineManagements, updateSupport, loginUser.getUsername());
        return AjaxResult.success(message);
    }

}
